<template>
  <el-main>
    <div class="tool-header">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input v-model="search" style="width:200px;margin-right:10px" size="mini" placeholder="请输入关键字检索" />
          <el-button type="primary" size="mini" @click="flush()">查询</el-button>
          <el-button type="success" size="mini" icon="el-icon-circle-plus" @click="edit()">创建</el-button>
        </el-col>
      </el-row>
    </div>
    <el-dialog
      :visible.sync="dialog"
      width="400px"
      title="创建ApiResource"
      style="margin:15px"
      @close="close"
    >
      <el-form ref="claimForm" label-width="100px" label-position="left" :model="apisecret">
        <el-form-item label="值:" prop="value">
          <el-input v-model="apisecret.value" size="mini" />
        </el-form-item>
        <el-form-item label="类型:" prop="type">
          <el-select v-model="apisecret.type" size="mini" placeholder="请选择">
            <el-option v-for="(item,index) in secretTypes" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述:" prop="description">
          <el-input v-model="apisecret.description" size="mini" />
        </el-form-item>
        <el-form-item label="哈希类型:" prop="hash">
          <el-select v-model="apisecret.hash" size="mini" placeholder="请选择">
            <el-option
              v-for="(item,index) in hashTypes"
              :key="index"
              :label="item.type"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <div class="dialog-footer el-message-box__btns">
          <el-button type="info" size="mini" @click="close(false)">取消</el-button>
          <el-button type="primary" size="mini" @click="Submit()">确认</el-button>
        </div>
      </el-form>
    </el-dialog>
    <el-table
      ref="multipleTable"
      :data="apisecrets"
      tooltip-effect="dark"
      size="mini"
      border
      align="center"
      style="width:100%"
      :stripe="true"
    >
      <el-table-column width="50px" align="center" prop="id" label="序号" />
      <el-table-column prop="type" label="密钥类型" />
      <el-table-column prop="value" label="值" />
      <el-table-column prop="description" label="描述" />

      <el-table-column label="操作" align="center" width="100px">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeScope(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page_footer_box"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"
      :current-page.sync="currentPage"
      :page-sizes="[1,10,30,50]"
      :page-size.sync="pageSize"
      @current-change="flush()"
      @size-change="flush()"
    />
  </el-main>
</template>
<script>
export default {
  data() {
    return {
      dialog: false, // 创建模态窗口
      hashTypes: [{ type: 'sha256', value: 0 }, { type: 'sha512', value: 1 }],
      apisecret: {
        description: 'string',
        value: 'string',
        expiration: '2019-05-21T07:39:11.945Z',
        hash: 0,
        type: 'string',
        resourceName: 'string'
      },
      totalCount: 0,
      pageSize: 10,
      currentPage: 1,
      search: '',
      apisecrets: [],
      secretTypes: [],
      apiresourceId: this.$route.query.apiresourceId,
      clientId: this.$route.query.clientId
    }
  },
  async mounted() {
    await this.flush()
  },
  methods: {
    async edit(row) {
      if (row) {
        this.apisecret = row
      } else {
        this.apisecret = {}
      }
      await this.getSecretTypes()
      this.dialog = true
    },
    async getSecretTypes() {
      const res = await this.$http.get('api/Infrastructure/GetSecretTypes')
      if (res) {
        this.secretTypes = res
        this.apisecret.type = res[0]
      }
    },
    removeScope(row) {
      this.$confirm('确认删除所选用户?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          let result = ''
          if (this.apiresourceId) {
            result = await this.$http.post(
              'api/ApiResource/RemoveSecret',
              row.id
            )
          }
          if (this.clientId) {
            result = await this.$http.post('api/Client/RemoveSecret', row.id)
          }
          if (result) {
            this.$message({
              showClose: true,
              type: 'success',
              message: '删除成功!'
            })
          }

          this.flush()
        })
        .catch(() => {
          this.$message({
            showClose: true,
            type: 'info',
            message: '已取消删除!'
          })
        })
    },
    async close() {
      this.dialog = false

      await this.flush()
    },
    async Submit() {
      if (this.apiresourceId) {
        this.apisecret.apiResourceId = this.apiresourceId
        const res = await this.$http.post(
          'api/ApiResource/AddApiSecret',
          this.apisecret
        )

        if (res) {
          this.$message({
            message: res,
            type: 'success'
          })
        }
      }
      if (this.clientId) {
        this.apisecret.clientId = this.clientId
        const res = await this.$http.post(
          'api/Client/AddApiSecret',
          this.apisecret
        )

        if (res) {
          this.$message({
            message: res,
            type: 'success'
          })
        }
      }
      this.close()
    },
    async flush() {
      if (this.apiresourceId) {
        const result = await this.$http.get('api/ApiResource/GetApiSecrets', {
          Search: this.search,
          isdesc: true,
          Page: this.currentPage,
          PageSize: this.pageSize,
          apiresourceId: this.apiresourceId
        })
        this.totalCount = result.totalCount
        this.apisecrets = result.data
      }
      if (this.clientId) {
        const result = await this.$http.get('api/Client/GetApiSecrets', {
          Search: this.search,
          isdesc: true,
          Page: this.currentPage,
          PageSize: this.pageSize,
          clientId: this.clientId
        })
        this.totalCount = result.totalCount
        this.apisecrets = result.data
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.el-tag {
  margin: 2px;
}

</style>
